package com.jingding.procurement.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jingding.procurement.enumeration.EntityEnum;
import com.jingding.procurement.mapper.ProcurementContractMapper;
import com.jingding.procurement.pojo.entity.Entity;
import com.jingding.procurement.pojo.entity.ProcurementContractEntity;
import com.jingding.procurement.pojo.vo.ProcurementContractVO;
import com.jingding.procurement.service.ProcurementContractService;
import com.jingding.procurement.util.IO;
import com.jingding.procurement.util.Requesting;
import com.jingding.procurement.web.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

@Service
@Slf4j
public class ProcurementContractServiceImpl implements ProcurementContractService {
    @Autowired
    private ProcurementContractMapper mapper;

    @Value("${PCE_url}")
    private String PCE_url;

    @Override
    public JsonResult pushProcurementContract(Entity entity, String secondTime) throws IOException, ClassNotFoundException {
        List<ProcurementContractVO> procurementContractVOS = mapper.pushProcurementContract(entity.getStartTime(), entity.getEndTime());
        if (procurementContractVOS.size()<1){
            log.debug("暂无[采购合同],下次任务开始时间不做变更,时间为:{} ,暂不推送数据  " , secondTime);;
            return JsonResult.ok("暂无[采购合同],下次任务开始时间不做变更 时间为:  " + secondTime);
        }
        String jsonObject=null;
        int batchSize = 100;
        for (int i = 0; i < procurementContractVOS.size(); i += batchSize) {
            List<ProcurementContractVO> batch = procurementContractVOS.subList(i,
                    Math.min(i + batchSize, procurementContractVOS.size()));
            jsonObject = Requesting.httpPost(PCE_url,
                    JSON.toJSONString(batch, SerializerFeature.WriteNullStringAsEmpty));
        }
        log.debug("[采购合同]响应:{}",jsonObject);
        ProcurementContractEntity procurementContractEntity = IO.procurementContractRead();
        if (JSON.parseObject(jsonObject).getObject("code", (Type) String.class).equals("200")) {
            procurementContractEntity.setStartTime(secondTime);
            IO.procurementContractWrite(procurementContractEntity);
            log.debug("[采购合同]起始时间修改完成:{}", secondTime);
            log.debug("[采购合同]条数:{} ,{}推送完成", procurementContractVOS.size(),secondTime);
            return JsonResult.ok("[采购合同]数据推送完成,推送条数: "+procurementContractVOS.size()+". 推送时间: "+secondTime);
        }
        log.debug("[采购合同]推送失败: {}",jsonObject);
        return JsonResult.fail(400, "[采购合同]推送失败,下次任务开始时间为:  " + procurementContractEntity.getStartTime()+"失败原因: "+jsonObject+"\\n" +
                "推送内容:"+JSON.toJSONString(procurementContractVOS, SerializerFeature.WriteNullStringAsEmpty));

    }

}
